package cn.wtg.kotlinlib.baseViewModel

import android.content.Context
import android.util.Log
import androidx.lifecycle.LifecycleEventObserver
import com.lxj.xpopup.impl.LoadingPopupView
import com.lxj.xpopup.XPopup
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Lifecycle
import android.view.View
import cn.wtg.kotlinlib.eventBus.Event
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.Timer
import kotlin.concurrent.schedule

/**
 * author: wtg
 * date:2020/3/13 0013
 * desc: ViewModel 生命周期管理的基类
 */
abstract class BaseLifecycleVM : BaseVM(), LifecycleEventObserver {
    private var loadingPopupView: LoadingPopupView? = null

    /**
     * 显示加载框
     *
     * @param context 上下文
     * @param title 标题
     */
    fun showLoading(context: Context, title: String) {
        CoroutineScope(Dispatchers.Main).launch {
            if (loadingPopupView == null) {
                loadingPopupView = XPopup.Builder(context).dismissOnBackPressed(false)
                    .dismissOnTouchOutside(false).asLoading()
            }
            loadingPopupView?.setTitle(title)
            if (!loadingPopupView?.isShow!!) {
                loadingPopupView!!.show()
            }
        }
    }

    /**
     * 显示加载框
     */
    protected fun showLoading(context: Context) {
        showLoading(context,"")
    }

    /**
     * 是否正在加载
     * @return true 是 false否
     */
    val isLoading: Boolean
        get() = if (loadingPopupView == null) {
            false
        } else loadingPopupView!!.isShow

    /**
     * 关闭加载框 当网络请求太快，为了更好的呈现效果
     * 可以适当给个延迟
     */
    fun closeLoading() {
        Timer().schedule(200){
            if (loadingPopupView != null && loadingPopupView!!.isShow) {
                loadingPopupView!!.dismiss()
            }
        }
    }

    /**
     * 按钮点击事件
     *
     * @param view view
     */
    open fun onViewClick(view: View?) {}
    override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
        when (event) {
            Lifecycle.Event.ON_CREATE -> onCreate()
            Lifecycle.Event.ON_START -> onStart()
            Lifecycle.Event.ON_RESUME -> onResume()
            Lifecycle.Event.ON_PAUSE -> onPause()
            Lifecycle.Event.ON_STOP -> onStop()
            Lifecycle.Event.ON_DESTROY -> onDestroy()
            else -> {

            }
        }
    }

    /**
     * 注册eventBus
     */
    protected fun registerEventBus() {
        EventBus.getDefault().register(this)
    }

    /**
     * 注销eventBus
     */
    private fun unregisterEventBus(){
        EventBus.getDefault().unregister(this)
    }

    /**
     * 消息通知
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun onMessageEvent(event: Event?) {
    }

   open fun onCreate() {
        Log.e(TAG,"onCreate")
    }

    open fun onStart() {
        Log.e(TAG,"onStart")
    }

    open fun onResume() {
        Log.e(TAG,"onResume")
    }

    open fun onPause() {
        Log.e(TAG,"onPause")
    }

    open fun onStop() {
        Log.e(TAG,"onStop")
    }

    open fun onDestroy() {
//        Log.e(TAG,"onDestroy")
        closeLoading()
        unregisterEventBus()
        loadingPopupView = null
    }
}